﻿using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Security.Cryptography;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using AnyOffice.DAL;
using  AnyOffice.Components ;

namespace AnyOffice
{
    public partial class login : Page
    {
        public bool loginKey; 
         
        private void GetMessage(string message)
        {
            this.litPromat.Text ="<font color='red'>"+ message+"</font>";
            StringBuilder builder = new StringBuilder();
            builder.Append("<script type=\"text/javascript\" language=\"javascript\">\n");
            builder.Append("if(popMessage){alert('" + message + "');}\n");
            builder.Append("</script>\n"); 
            ClientScript.RegisterStartupScript(this.GetType(), "pop", builder.ToString());
        } 

        protected void Page_Load(object sender, EventArgs e)
        {
           

        }

        protected void btnLogin_Click(object sender, ImageClickEventArgs e)
        {
            this.litTitle.InnerText = InterfaceCache.StatusText;
            ConfigManager manager = new ConfigManager();
            this.loginKey = manager.LoginKey;
            string userId = string.Empty;
            string password = string.Empty;
            string verifyCode = string.Empty;
            
                Response.AppendHeader("Expires", DateTime.Now.AddDays(-1.0).ToString("r"));
                Response.AppendHeader("Pragma", "no-cache");
                Response.AppendHeader("Cache-Control", "no-cache");
                Random random = new Random(DateTime.Now.Millisecond);
             
            
                userId = this.txtUserId.Text.ToLower();
                password = FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtPassword.Text, "md5");

                string str = string.Empty;
                string str2 = string.Empty;
                bool flag = false;
                string str3 = string.Empty;
                SqlConnection conn = new SqlConnection(ConfigManager.ConnectionString);

                if (str == string.Empty)
                {
                    DataTable table2 = new UsersDao().Login(conn, userId, password);
                    if (table2.Rows.Count <= 0)
                    {
                        SysLog.Log(conn, userId, 2, "");
                        this.GetMessage("用户名或密码错误,请重新输入！");
                        return;
                    }
                    str2 = table2.Rows[0]["UserId"].ToString();
                    flag = Convert.ToBoolean(table2.Rows[0]["MenuType"].ToString());
                    if (table2.Rows[0]["BindIp"] != DBNull.Value)
                    {
                        str3 = table2.Rows[0]["BindIp"].ToString();
                    }
                    if (Convert.ToBoolean(table2.Rows[0]["Disabled"].ToString()))
                    {
                        SysLog.Log(conn, userId, 16, "");
                        this.GetMessage("帐号已禁用！");
                        return;
                    }

                }
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
                if (str != string.Empty)
                {
                    userId = str;
                }
                else
                {
                    userId = str2;
                }
                if ((str3 != string.Empty) && (str3.IndexOf(Request.UserHostAddress) == -1))
                {
                    SysLog.Log(conn, userId, 9, "");
                    this.GetMessage("非法IP登录！");
                }
                else
                {
                    SysLog.Log(conn, userId, 1, "");
                    FormsAuthentication.SetAuthCookie(userId, true);
                    if (manager.SecurityRember)
                    {
                        HttpCookie cookie = new HttpCookie("AnyOffice_Rember_UserId", Server.UrlEncode(userId));
                        cookie.Expires = DateTime.Now.AddMonths(1);
                        Response.Cookies.Add(cookie);
                    }
                    UserCache.Update(userId);
                  
                    
                     Response.Redirect("general/index.aspx");
                    
                }
            
        }




    }
}
